package document;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Vector;

/**
 * Maintains a dictionary of all the stop words. 
 * @author severin
 *
 */
public class StopWordLexicon {
	
	static public StopWordLexicon getInstance()
	{
		if(instance == null)
		{
			instance = new StopWordLexicon(STOP_WORDS_FILE);
		}
		return instance;
	}
	
	private StopWordLexicon(String filename)
	{
		stopWords = new HashSet<String>();
		try {
			BufferedReader reader = new BufferedReader(new FileReader(filename));
			String line = "";

			while ((line = reader.readLine()) != null) {
				stopWords.add(line);
			}
			reader.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Is word a stop word? Case insensitive.
	 * @param word 
	 */
	public boolean isStopWord(String word)
	{
		return stopWords.contains(word.toLowerCase()) && !isDisabled;
	}
	
	
	/**
	 * For testing only, to allow to turn of stop words recognition temporarily
	 * @param flag
	 */
	public static void setDisabled(boolean flag)
	{
		isDisabled = flag;
	}
	
	public Vector<String> getAllStopWords() {
		Iterator<String> it = stopWords.iterator();
		Vector<String> allWords = new Vector<String>();
		while (it.hasNext()) {
			allWords.add(it.next());
		}
		return allWords;
	}
	
	
	HashSet<String> stopWords;
	
	static final private String STOP_WORDS_FILE = "TIME/English_Stopwords.txt";
	static private StopWordLexicon instance;
	static private boolean isDisabled = false;
}
